/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EventProcessor;

import ButtonBeans.ButtonBean;
import ButtonBeans.ControllerElement;
import ButtonBeans.JoystickBean;
import ButtonBeans.RoundButtonBean;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author luc0
 */
public class EventProcessor {

    private static EventProcessor instance;
    private Map buttonToPressMap = new HashMap<String, Integer>();
    private Map threadMap = new HashMap<String, Thread>();
    private Map buttonMap = new HashMap<String, ControllerElement>();
    private Robot robot;
    private KeyMapper keyMapper;

    public static EventProcessor getInstance() {

        if (instance == null) {
            try {
                instance = new EventProcessor();
                instance.robot = new Robot();
                instance.keyMapper = new KeyMapper();
            } catch (AWTException ex) {
                Logger.getLogger(EventProcessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return instance;
    }

    public void mapIdsToButtons(List<ControllerElement> buttonList) {
        if (buttonList == null) {
            return;
        }
        for (ControllerElement element : buttonList) {
            if(element instanceof RoundButtonBean || element instanceof ButtonBean){
                buttonToPressMap.put(String.valueOf(element.getButtonId()), keyMapper.getKeyEventValueFromString(element.getButtonToPress()));
                buttonMap.put(String.valueOf(element.getButtonId()), element);
            } else if(element instanceof JoystickBean){
                 buttonToPressMap.put("up", keyMapper.getKeyEventValueFromString(((JoystickBean)element).getUpButton()));
                 buttonToPressMap.put("left", keyMapper.getKeyEventValueFromString(((JoystickBean)element).getLeftButton()));
                 buttonToPressMap.put("right", keyMapper.getKeyEventValueFromString(((JoystickBean)element).getRightButton()));
                 buttonToPressMap.put("down", keyMapper.getKeyEventValueFromString(((JoystickBean)element).getDownButton()));
            }
            


        }
    }

    public void pressButton(String buttonId) {


        if (((ControllerElement) buttonMap.get(buttonId)).isRepeatsPresses()) {
            if (!threadMap.containsKey(buttonId)) {
                Thread thread = new Thread(new KeyPresserThread((Integer) buttonToPressMap.get(buttonId), ((ControllerElement) buttonMap.get(buttonId)).getTimeBetweenPresses()));
                threadMap.put(buttonId, thread);
                thread.start();
            }
        } else {
            if ((Integer) buttonToPressMap.get(buttonId) == -1) {
                System.out.println("MOUS 1 PRESS");
                robot.mousePress(InputEvent.BUTTON1_MASK);
            } else if ((Integer) buttonToPressMap.get(buttonId) == -2) {
                System.out.println("MOUS 2 PRESS");
                robot.mousePress(InputEvent.BUTTON3_MASK);
            } else {
                robot.keyPress((Integer) buttonToPressMap.get(buttonId));
            }


        }
    }

    public void releaseButton(String buttonId) {

        if (((ControllerElement) buttonMap.get(buttonId)).isRepeatsPresses()) {
            if (threadMap.containsKey(buttonId)) {
                ((Thread) threadMap.get(buttonId)).stop();
                threadMap.remove(buttonId);
            }
        } else {
            if ((Integer) buttonToPressMap.get(buttonId) == -1) {
                System.out.println("MOUS 1 RELEASE");

                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            } else if ((Integer) buttonToPressMap.get(buttonId) == -2) {
                System.out.println("MOUS 2 RELEASE");
                robot.mouseRelease(InputEvent.BUTTON3_MASK);
            } else {
                robot.keyRelease((Integer) buttonToPressMap.get(buttonId));
            }

        }

    }

    public void joystickPressed(String joystickId){

          if(joystickId.equals("leftUp")){
             robot.keyPress((Integer) buttonToPressMap.get("left"));
             robot.keyPress((Integer) buttonToPressMap.get("up"));
          } else if(joystickId.equals("rightUp")){
             robot.keyPress((Integer) buttonToPressMap.get("right"));
             robot.keyPress((Integer) buttonToPressMap.get("up"));
          }else if(joystickId.equals("leftDown")){
             robot.keyPress((Integer) buttonToPressMap.get("left"));
             robot.keyPress((Integer) buttonToPressMap.get("down"));
          }else if(joystickId.equals("rightDown")){
             robot.keyPress((Integer) buttonToPressMap.get("right"));
             robot.keyPress((Integer) buttonToPressMap.get("down"));
          }else{
              robot.keyPress((Integer) buttonToPressMap.get(joystickId));
          }
          

    }

    public void joystickReleased(String joystickId){
         if(joystickId.equals("leftUp")){
             robot.keyRelease((Integer) buttonToPressMap.get("left"));
             robot.keyRelease((Integer) buttonToPressMap.get("up"));
          } else if(joystickId.equals("rightUp")){
             robot.keyRelease((Integer) buttonToPressMap.get("right"));
             robot.keyRelease((Integer) buttonToPressMap.get("up"));
          }else if(joystickId.equals("leftDown")){
             robot.keyRelease((Integer) buttonToPressMap.get("left"));
             robot.keyRelease((Integer) buttonToPressMap.get("down"));
          }else if(joystickId.equals("rightDown")){
             robot.keyRelease((Integer) buttonToPressMap.get("right"));
             robot.keyRelease((Integer) buttonToPressMap.get("down"));
          }else{
              robot.keyRelease((Integer) buttonToPressMap.get(joystickId));
          }
    }
}
